package com.dashunwms_aohe.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.dashunwms_aohe.bean.ds.*;
import com.dashunwms_aohe.dao.*;
import com.dashunwms_aohe.service.DSBackWriteDataBaseService;
import javassist.compiler.ast.Variable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j(topic = "DSBackWriteDataBaseServiceImpl")
public class DSBackWriteDataBaseServiceImpl implements DSBackWriteDataBaseService {
    @Resource
    private ZxyDsysjlResdao zxyDsysjlResdao;

    @Override
    public int insertYSJLToDataBase(List<DS_InStockYSJLRes> list) {
        int restultCount = 0;
        for (DS_InStockYSJLRes dsInStockYSJLRes : list) {
            DS_InStockYSJLRes ysjlByBillIdAndBilldtlid = zxyDsysjlResdao.getYSJLByBillIdAndBilldtlid(dsInStockYSJLRes.getBillid(), dsInStockYSJLRes.getBilldtlid());
            if (BeanUtil.isEmpty(ysjlByBillIdAndBilldtlid)) {
                int insert = zxyDsysjlResdao.insert(dsInStockYSJLRes);
                restultCount = restultCount + insert;
            } else {
                log.error("验收记录千方数据库中已经存在,无需在插入" + JSON.toJSONString(dsInStockYSJLRes));
            }
        }
        return restultCount;
    }

    @Resource
    private ZxyDsshjlResdao zxyDsshjlResdao;

    @Override
    public int insertSHJLToDataBase(List<DS_InStockSHJLRes> list) {
        int restultCount = 0;
        for (DS_InStockSHJLRes inStockSHJLRes : list) {
            DS_InStockSHJLRes shjlByBillIdAndBilldtlid = zxyDsshjlResdao.getSHJLByBillIdAndBilldtlid(inStockSHJLRes.getBillid(), inStockSHJLRes.getBilldtlid());
            if (BeanUtil.isEmpty(shjlByBillIdAndBilldtlid)) {
                int insert = zxyDsshjlResdao.insert(inStockSHJLRes);
                restultCount = restultCount + insert;
            } else {
                log.error("收货记录千方数据库中已经存在,无需在插入" + JSON.toJSONString(inStockSHJLRes));
            }
        }
        return restultCount;
    }

    @Resource
    private ZxyShjushouResdao zxyShjushouResdao;

    @Override
    public int insertSHJSJLToDataBase(List<DS_InStockJSJLRes> list) {
        int restultCount = 0;
        for (DS_InStockJSJLRes inStockJSJLRes : list) {
            DS_InStockJSJLRes shjlByBillIdAndBilldtlid = zxyShjushouResdao.getJSJLByBillIdAndBilldtlid(inStockJSJLRes.getBilldtlid(), inStockJSJLRes.getBillid());
            if (BeanUtil.isEmpty(shjlByBillIdAndBilldtlid)) {
                int insert = zxyShjushouResdao.insert(inStockJSJLRes);
                restultCount = restultCount + insert;
            } else {
                log.error("拒收千方数据库中已经存在,无需在插入" + JSON.toJSONString(inStockJSJLRes));
            }
        }
        return restultCount;
    }

    @Resource
    private ZxyDsXTysjlResdao zxyDsXTysjlResdao;

    @Override
    public int insertXTYSJLToDataBase(List<DS_InStockXTYSJLRes> list) {
        int restultCount = 0;
        for (DS_InStockXTYSJLRes dsInStockXTYSJLRes : list) {
            DS_InStockXTYSJLRes xtysjlByBillIdAndBilldtlid = zxyDsXTysjlResdao.getXTYSJLByBillIdAndBilldtlid(dsInStockXTYSJLRes.getBillid(), dsInStockXTYSJLRes.getBilldtlid());
            if (BeanUtil.isEmpty(xtysjlByBillIdAndBilldtlid)) {
                int insert = zxyDsXTysjlResdao.insert(dsInStockXTYSJLRes);
                restultCount = restultCount + insert;
            } else {
                log.error("销售退货验收记录千方数据库中已经存在,无需在插入" + JSON.toJSONString(dsInStockXTYSJLRes));
            }
        }
        return restultCount;
    }

    @Resource
    private ZxyBhgYSJLResdao bhgYSJLResdao;

    @Override
    public int insertBHGYSJLToDataBase(List<DS_InStockBHGYSJLRes> list) {
        int restultCount = 0;
        for (DS_InStockBHGYSJLRes dsInStockBHGYSJLRes : list) {
            DS_InStockBHGYSJLRes bhgysjlByBillIdAndBilldtlid = bhgYSJLResdao.getBHGYSJLByBillIdAndBilldtlid(dsInStockBHGYSJLRes.getBillid(), dsInStockBHGYSJLRes.getBilldtlid());
            if (BeanUtil.isEmpty(bhgysjlByBillIdAndBilldtlid)) {
                int insert = bhgYSJLResdao.insert(dsInStockBHGYSJLRes);
                restultCount = restultCount + insert;
            } else {
                log.error("不合格验收记录千方数据库中已经存在,无需在插入" + JSON.toJSONString(dsInStockBHGYSJLRes));
            }
        }
        return restultCount;
    }

    @Resource
    private ZxyDsckfhResdao zxyDsckfhResdao;

    @Override
    public int insertCKFHToDataBase(List<DS_OutStockFHRes> list) {
        int restultCount = 0;
        for (DS_OutStockFHRes dsOutStockFHRes : list) {
            DS_OutStockFHRes ckfhByBillIdAndBilldtlid = zxyDsckfhResdao.getCKFHByBillIdAndBilldtlid(dsOutStockFHRes.getBILLID(), dsOutStockFHRes.getBilldtlid());
            if (BeanUtil.isEmpty(ckfhByBillIdAndBilldtlid)) {
                int insert = zxyDsckfhResdao.insert(dsOutStockFHRes);
                restultCount = restultCount + insert;
            } else {
                log.error("出库复核记录千方数据库中已经存在,无需在插入" + JSON.toJSONString(dsOutStockFHRes));
            }
        }
        return restultCount;
    }

    @Resource
    private ZxyJTOutStockJLdao zxyJTOutStockJLdao;

    @Override
    public int insertCGTHJLToDataBase(List<DS_JTOutStockJLRes> list) {
        int restultCount = 0;
        for (DS_JTOutStockJLRes dsJtOutStockJLRes : list) {
//            DS_OutStockFHRes ckfhByBillIdAndBilldtlid = zxyDsckfhResdao.getCKFHByBillIdAndBilldtlid(dsJtOutStockJLRes.getPKH_OWNER_NO(), dsJtOutStockJLRes.getPKL_OWNER_SYSTEM_LINE_NO());
            DS_OutStockFHRes ckfhByBillIdAndBilldtlid = null;
            if (BeanUtil.isEmpty(ckfhByBillIdAndBilldtlid)) {
                int insert = zxyJTOutStockJLdao.insert(dsJtOutStockJLRes);
                restultCount = restultCount + insert;
            } else {
                log.error("出库复核记录千方数据库中已经存在,无需在插入" + JSON.toJSONString(dsJtOutStockJLRes));
            }
        }
        return restultCount;
    }

    @Resource
    private ZxyDswtysjlResdao zxyDswtysjlResdao;

    @Override
    public int insertWTYSJLToDataBase(List<DS_WTYSJLRes> list) {
        int restultCount = 0;
        for (DS_WTYSJLRes dsWtysjlRes : list) {
            DS_WTYSJLRes wtysjlBylohOwnerNo = zxyDswtysjlResdao.getWTYSJLByloh_owner_no(dsWtysjlRes.getLoh_owner_no(), dsWtysjlRes.getLOH_LOGISTICS_NO());
            if (BeanUtil.isEmpty(wtysjlBylohOwnerNo)) {
                String replace = dsWtysjlRes.getFcwd().replace(" ", "");
                dsWtysjlRes.setFcwd(replace);
                int insert = zxyDswtysjlResdao.insert(dsWtysjlRes);
                restultCount = restultCount + insert;
            } else {
                log.error("运输记录千方数据库中已经存在,无需在插入" + JSON.toJSONString(dsWtysjlRes));
            }
        }
        return restultCount;
    }

    @Resource
    private ZxyDsstockResdao zxyDsstockResdao;

    @Override
    public int insertStockToDataBase(List<DS_StockRes> list) {
        int restultCount = 0;
        //TODO 库存这里正常逻辑应该是每次拉取的时候,都应该把现在的库存清空掉,重新插入最新库存
        int deleteCount = zxyDsstockResdao.deleteAll();
        log.info("执行插入大舜库存前一共删除库存: [" + deleteCount + "]条库存");
        for (DS_StockRes dsStockRes : list) {
            int insert = zxyDsstockResdao.insert(dsStockRes);
            restultCount = restultCount + insert;

        }
        return restultCount;
    }

    @Resource
    private ZxyDsStcokBHGResdao dsStcokBHGResdao;

    @Override
    public int insertStockBHGToDataBase(List<DS_StockBHGRes> list) {
        int restultCount = 0;
        for (DS_StockBHGRes dsStockBHGRes : list) {
            int insert = dsStcokBHGResdao.insert(dsStockBHGRes);
            restultCount = restultCount + insert;
        }
        return restultCount;
    }

    @Resource
    private ZxyDsstockYHJLResdao zxyDsstockYHJLResdao;

    @Override
    public int insertStockYHJLToDataBase(List<DS_StockYHJLRes> list) {
        int restultCount = 0;
        for (DS_StockYHJLRes dsStockYHJLRes : list) {
            DS_StockYHJLRes yhjlByBillIdAndBilldtlid = zxyDsstockYHJLResdao.getYHJLByBillIdAndBilldtlid(dsStockYHJLRes.getBillid(), dsStockYHJLRes.getBILLDTLID());
            if (BeanUtil.isEmpty(yhjlByBillIdAndBilldtlid)) {
                int insert = zxyDsstockYHJLResdao.insert(dsStockYHJLRes);
                restultCount = restultCount + insert;
            } else {
                log.error("养护记录千方数据库中已经存在,无需在插入" + JSON.toJSONString(dsStockYHJLRes));
            }


        }
        return restultCount;
    }
}
